package com.eam.asset.category.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.eam.asset.category.dto.AssetCategoryDto;
import com.eam.asset.category.vo.AssetCategoryAddVo;
import com.eam.asset.category.vo.AssetCategoryPutVo;
import com.eam.asset.category.vo.AssetCategoryQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eam.common.annotation.Log;
import com.eam.common.core.controller.BaseController;
import com.eam.common.core.domain.AjaxResult;
import com.eam.common.enums.BusinessType;
import com.eam.asset.category.service.IAssetCategoryService;
import com.eam.common.utils.poi.ExcelUtil;
import com.eam.common.core.page.TableDataInfo;

/**
 * 资产档案类别信息Controller
 *
 * @author tzf
 * @date 2023-10-31
 */
@RestController
@RequestMapping("/asset/category")
@Validated
@Api(tags = "资产类型控制器")
public class AssetCategoryController extends BaseController {
    private final IAssetCategoryService assetCategoryService;

    //构造器注入
    @Autowired
    public AssetCategoryController(IAssetCategoryService assetCategoryService) {
        this.assetCategoryService = assetCategoryService;
    }

    /**
     * 查询资产档案类别信息列表
     */
    @PreAuthorize("@ss.hasPermi('asset:category:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取资产列表")
    public TableDataInfo list(AssetCategoryQueryVo assetCategory) {
        startPage();
        List<AssetCategoryDto> list = assetCategoryService.selectAssetCategoryList(assetCategory);
        return getDataTable(list);
    }

    /**
     * @param type 类型
     * @description: 类型对应的具体int值, 如房屋类型:0,为空时返回根分类
     * @author tzf
     * @date: 2023/11/9 19:41
     */

    @PreAuthorize("@ss.hasPermi('asset:category:query')")
    @GetMapping("/list/root")
    @ApiOperation(value = "获取资产分类列表")
    public AjaxResult GetRootCategoryTypeList(@RequestParam(required = false) String type) {
        return success(assetCategoryService.GetRootCategoryTypeList(type));
    }

    /**
     * 导出资产档案类别信息列表
     */
    @PreAuthorize("@ss.hasPermi('asset:category:export')")
    @Log(title = "资产档案类别信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出Excel")
    public void export(HttpServletResponse response, AssetCategoryQueryVo assetCategory) {
        List<AssetCategoryDto> list = assetCategoryService.selectAssetCategoryList(assetCategory);
        ExcelUtil<AssetCategoryDto> util = new ExcelUtil<AssetCategoryDto>(AssetCategoryDto.class);
        util.exportExcel(response, list, "资产档案类别信息数据");
    }

    /**
     * 获取资产档案类别信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('asset:category:query')")
    @GetMapping(value = "/{categoryId}")
    @ApiOperation(value = "查询分类:根据ID")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId) {
        return success(assetCategoryService.selectAssetCategoryByCategoryId(categoryId));
    }

    /**
     * 新增资产档案类别信息
     */
    @PreAuthorize("@ss.hasPermi('asset:category:add')")
    @Log(title = "资产档案类别信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "添加分类")
    public AjaxResult add(@RequestBody AssetCategoryAddVo assetCategory) {
        return toAjax(assetCategoryService.insertAssetCategory(assetCategory));
    }

    /**
     * 修改资产档案类别信息
     */
    @PreAuthorize("@ss.hasPermi('asset:category:edit')")
    @Log(title = "资产档案类别信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改分类类别")
    public AjaxResult edit(@Validated @RequestBody AssetCategoryPutVo assetCategory) {
        return toAjax(assetCategoryService.updateAssetCategory(assetCategory));
    }

    /**
     * 删除资产档案类别信息
     */
    @PreAuthorize("@ss.hasPermi('asset:category:remove')")
    @Log(title = "资产档案类别信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    @ApiOperation(value = "删除分类:根据id")
    public AjaxResult remove(@PathVariable Long[] categoryIds) {
        return toAjax(assetCategoryService.deleteAssetCategoryByCategoryIds(categoryIds));
    }
}
